package api.action;

import api.model.ApiAuthinfo;
import api.service.ApiAuthinfoService;
import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.ListButton;
import m.html.list.ListElement;
import m.html.list.SuccessMethod;
import m.system.cache.CacheUtil;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.action.StatusAction;

@ActionMeta(name="api/apiAuthinfo")
public class ApiAuthinfoAction extends StatusAction {
    private ApiAuthinfo model;

    public JSONMessage doSave(){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getStatusPower());
            String msg=getService(ApiAuthinfoService.class).save(model);
            result.push("model", model);
            fillJSONResult(result,true,msg);
        } catch (Exception e) {
            fillJSONResult(result,false,e);
        }
        return result;
    }
    public VueResult toEdit() throws Exception {
        verifyMenu("A2111");
        verifyAdminOperPower(getStatusPower());
        if(null!=model&&!StringUtil.isSpace(model.getAuthId())){
            model=getService(ApiAuthinfoService.class).get(model.getAuthId());
        }else {
            model=new ApiAuthinfo();
            model.setAdmin(getSessionAdmin());
            model.setOrg(getSessionOrgGroup());
            model.setAuthId(GenerateID.upsetKey(GenerateID.generatePrimaryKey()).toLowerCase());
            model.setAuthKey(
                GenerateID.upsetKey(GenerateID.upsetKey(
                    GenerateID.upsetKey(GenerateID.generatePrimaryKey())
                    +GenerateID.upsetKey(GenerateID.tempKey())
                )).toLowerCase()
            );
            model.setAuthTime("10");
            model.setType("B");
        }
        return new VuePageResult("page/base/api/editApiAuthinfo.js")
                .addParam("model",model);
    }
    public VueResult toApiAuthinfoInterList() throws Exception {
        model=getService(ApiAuthinfoService.class).get(model.getAuthId());
        return new VuePageResult("page/base/api/auth/apiAuthinfoInterList.js")
                .addParam("model",model);
    }
    public VueResult apiAuthinfoList() throws Exception{
        verifyMenu("A2111");
        boolean power=hasAdminOperPower(getStatusPower());
        VueListResult result=getVueListResult("鉴权信息登记", ApiAuthinfo.class,"action/api/apiAuthinfo/apiAuthinfoList");
        result.setSubTitle("可用通过鉴权信息访问系统接口，鉴权需要使用MD5加密，推荐后台访问使用该鉴权信息。");
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("名称", "name",true));
        result.addCol(toTextColumn("描述", "description",200,true));
        result.addCol(toTextColumn("登录人", "admin.realname",true));
        result.addCol(toTextColumn("登录机构", "org.name",true));
        result.addCol(toTextColumn("鉴权id", "authId",true));
        result.addCol(toTextColumn("鉴权key", "authKey",150,true));
        result.addCol(toTextColumn("鉴权误差", "authTime",true).setDictOption(ApiAuthinfo.class,"authTime"));
        result.addCol(toTextColumn("类型", "type",true).setDictOption(ApiAuthinfo.class,"type"));
        result.addCol(toStatusColumn(power,true));
        if(power) result.addCol(toSpaceColumn("操作",150,
            ListButton.modal("修改", ButtonType.PRIMARY,"action/api/apiAuthinfo/toEdit",900, SuccessMethod.REFRESH)
                .addParam("model.authId","authId"),
                ListElement.test("record.type=='B'",
                    ListButton.page("接口", ButtonType.SUCCESS,"action/api/apiAuthinfo/toApiAuthinfoInterList", SuccessMethod.REFRESH)
                        .addParam("model.authId","authId")
                )
        ).setFixed(Align.RIGHT));
        if(power) result.addButton(
                ListButton.modal("新增",ButtonType.PRIMARY,"action/api/apiAuthinfo/toEdit",900,SuccessMethod.REFRESH)
        );
        return result;
    }

    @Override
    public void doRecoveryCallback() throws Exception {
        model= ModelQueryList.getModel(model,"authId");
        CacheUtil.clear(ApiAuthinfo.class,model.getAuthId());
    }

    @Override
    public void doDisableCallback() throws Exception {
        model= ModelQueryList.getModel(model,"authId");
        CacheUtil.clear(ApiAuthinfo.class,model.getAuthId());
    }

    @Override
    public ApiAuthinfo getModel() {
        return model;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{"manage_api_power"};
    }

    public void setModel(ApiAuthinfo model) {
        this.model = model;
    }
}
